package com.tianhua.javs.client.bean;



import com.tianhua.javs.client.model.FieldModel;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

/**
 * Description:方法调用模型
 * date: 2021/10/21
 *
 * @author shenshuai
 * @version 1.0.0
 * @since JDK 1.8
 */
public class JavsScriptMethodBean {
   /**
    * 类名
    */
   private String className;

   /**
    * 组件名
    */
   private String compName;

   /**
    * 方法名
    */
   private String methodName;

   /**
    * 方法参数列表
    */
   private List<JavsParamBean> paramBeanList = new ArrayList<>();

   /**
    * 方法内容的javs描述---伪代码
    */
   private List<String> javsShellContentList;

   /**
    * 是否匹未配到原生方法
    */
   private boolean missOriginMethod;

   /**
    * 基于当前解析的方法要构建的一些变量
    *
    */
   private Map<String, List<ScriptVarMappingBean>> scriptVarMap;


   /**
    * 解析好的方法内容
    */
   private String javaMethodContent;

   /**
    * javs函数在翻译过程中产生的动态变量
    */
   private  List<ScriptVarMappingBean> runTimeVarList = new LinkedList<>();

   /**
    * 当前转换代码的内容需要导入的包
    */
   private List<String> packageList = new ArrayList<>();


   /**
    * 当前转换代码的内容需要导入的属性
    */
   private List<FieldModel> fieldModelList = new ArrayList<>();



   public List<ScriptVarMappingBean> getRunTimeVarList() {
      return runTimeVarList;
   }

   public String getJavaMethodContent() {
      return javaMethodContent;
   }

   public void setJavaMethodContent(String javaMethodContent) {
      this.javaMethodContent = javaMethodContent;
   }

   public Map<String, List<ScriptVarMappingBean>> getScriptVarMap() {
      return scriptVarMap;
   }

   public void setScriptVarMap(Map<String, List<ScriptVarMappingBean>> scriptVarMap) {
      this.scriptVarMap = scriptVarMap;
   }

   public boolean isMissOriginMethod() {
      return missOriginMethod;
   }

   public void setMissOriginMethod(boolean missOriginMethod) {
      this.missOriginMethod = missOriginMethod;
   }

   public List<JavsParamBean> getParamBeanList() {
      return paramBeanList;
   }

   public void setParamBeanList(List<JavsParamBean> paramBeanList) {
      this.paramBeanList = paramBeanList;
   }

   public String getClassName() {
      return className;
   }

   public void setClassName(String className) {
      this.className = className;
   }

   public String getCompName() {
      return compName;
   }

   public void setCompName(String compName) {
      this.compName = compName;
   }

   public String getMethodName() {
      return methodName;
   }

   public void setMethodName(String methodName) {
      this.methodName = methodName;
   }

   public List<String> getJavsShellContentList() {
      return javsShellContentList;
   }

   public void setJavsShellContentList(List<String> javsShellContentList) {
      this.javsShellContentList = javsShellContentList;
   }

   public void addRunTimeVar(ScriptVarMappingBean scriptVarMappingBean){
      this.runTimeVarList.add(scriptVarMappingBean);
   }


   /**
    * 根据javs变量名找到对应的类名
    * @param varName
    * @return
    */
   public String searchVarClassType(String varName){
      List<JavsParamBean> javsParamBeanList = this.getParamBeanList();
      String varClassType = "";
      //从参数中寻找
      if(!CollectionUtils.isEmpty(javsParamBeanList)){
         for (JavsParamBean javsParamBean : javsParamBeanList){
            if(javsParamBean.getJavsParamName().equals(varName.trim())){
               varClassType = javsParamBean.getParamType();
               break;
            }
         }
      }
      if(StringUtils.isNotEmpty(varClassType)){
         return varClassType;
      }

      //从运行时变量中寻找
      if(CollectionUtils.isNotEmpty(this.runTimeVarList)){
         for (ScriptVarMappingBean scriptVarMappingBean  : this.runTimeVarList){
            if(scriptVarMappingBean.getJavsVarCode().equals(varName.trim())){
               if(StringUtils.isNotEmpty(scriptVarMappingBean.getVarClassName())){
                  return scriptVarMappingBean.getVarClassName();
               }
               if(scriptVarMappingBean.getReferClassModel() != null){
                  return scriptVarMappingBean.getReferClassModel().getClassName();
               }
            }
         }
      }

      return null;
   }

   /**
    * 获取最新的运行时变量
    * @return
    */
   public ScriptVarMappingBean getLastRunTimeVar(){
      if(CollectionUtils.isNotEmpty(runTimeVarList)){
         return runTimeVarList.get(runTimeVarList.size() - 1);
      }
      return null;
   }

   /**
    * 在解析过程中动态收集需要导入的包信息
    * @param packageList
    */
   public void  addPackageList(List<String> packageList){
      if(CollectionUtils.isEmpty(packageList)){
         return;
      }

      for (String packageName : packageList){
         if(this.packageList.contains(packageName)){
            continue;
         }
         this.packageList.add(packageName);
      }
   }


   /**
    * 在解析过程中动态收集需要导入的包信息
    * @param packageName
    */
   public void  addPackage(String packageName){
      if(StringUtils.isEmpty(packageName)){
         return;
      }

      if(this.packageList.contains(packageName)){
         return;
      }
      this.packageList.add(packageName);

   }


   public List<String> getPackageList() {
      return packageList;
   }

   public List<FieldModel> getFieldModelList() {
      return fieldModelList;
   }

   public void addFieldModel(FieldModel fieldModel){
      boolean b = false;
      for (FieldModel fieldModel1 : fieldModelList){
         if(fieldModel1.getFieldName().equals(fieldModel.getFieldName())){
            b = true;
         }
      }
      if(!b){
         this.fieldModelList.add(fieldModel);
      }
   }


   /**
    * 去掉func {}
    * @return
    */
   public String getRefreshMethodContent(){
      if (StringUtils.isNotEmpty(this.javaMethodContent)){
         String [] contentArr = this.javaMethodContent.split("\n");
         List<String> contentList = new ArrayList<>();
         for (int i = 1;i < contentArr.length - 2;i ++){
            contentList.add(contentArr[i]);
         }
      }
      return "";
   }
}
